package com.jeesite.modules.app.course.exercise.record.web;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.course.exercises.user.entity.TcourseExercisesUser;
import com.jeesite.modules.zx.course.exercises.user.service.TcourseExercisesUserService;

@Controller
@RequestMapping(value = "/app/course/exercise/user/")
public class AppCourseExercisesUserController {
	@Autowired
	private TcourseExercisesUserService tcourseExercisesUserService;
	@RequestMapping(value="findPage")
	public void findPage(TcourseExercisesUser entity, HttpServletRequest request, HttpServletResponse response){
		String userId = request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		entity.setCreateBy(userId);
		Page<TcourseExercisesUser> findPage = tcourseExercisesUserService.findPage(entity);
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(findPage));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	
	@RequestMapping(value="save")
	public void save(TcourseExercisesUser entity, HttpServletRequest request, HttpServletResponse response){
		String userId = request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		entity.setCreateBy(userId);
		String courseId=entity.getCourseId();
		if(StringUtils.isBlank(courseId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "课程标识");
			return;
		}

		String courseHourseId=entity.getCourseHourseId();
		if(StringUtils.isBlank(courseHourseId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "课时标识");
			return;
		}
		TcourseExercisesUser model=new TcourseExercisesUser();
		model.setCreateBy(userId);
		model.setCourseId(courseId);
		model.setCourseHourseId(courseHourseId);
		List<TcourseExercisesUser> findList = tcourseExercisesUserService.findList(model);
		if(findList!=null&&findList.size()>0){
			entity=findList.get(0);
			JSONObject json=JSONObject.parseObject(JsonMapper.toJson(entity));
			AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
			return;
		}
		entity.setState(3);
		entity.setBarbecueTime(new Date());
		entity.setScore(new BigDecimal(0.0));
		tcourseExercisesUserService.save(entity);
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(entity));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
		
	}
}
